Write-Host "First read checklist.txt"
Set-Variable -name DistName 		-value launchmenot
Set-Variable -name WinRar		    -value "E:\Progs\WinRar\WinRar.exe"
#Set-Variable -name WinRarFilter		-value "-x*\.svn -x*\.svn\*"
Set-Variable -name WinRarOpts		-value "-r -s -m5 -inul"
Set-Variable -name NSISExe		    -value "E:\Progs\NSIS\makensis.exe"
Set-Variable -name NSISConfig		-value (Join-Path (Get-Location) "launchmenot.nsi")
Set-Variable -name SVNPATH  		-value "E:\Progs\Subversion\svn.exe"
Set-Variable -name DIRSRC 		    -value (Join-Path $env:UserProfile "\Documents\Coding\LaunchMeNot")
Set-Variable -name DIRCOMPILE 		-value (Join-Path $env:UserProfile "\Documents\Coding\LaunchMeNot\LaunchMeNot\bin\Release")
Set-Variable -name DIRDOTFUSCATED 	-value (Join-Path $env:UserProfile "\Documents\Coding\LaunchMeNot\LaunchMeNot\bin\Release")
Set-Variable -name DIRDOTFUSCATED 	-value (Join-Path $env:UserProfile "\Documents\Coding\LaunchMeNot\LaunchMeNot\bin\Release")
Set-Variable -name DIROUTPUT 		-value (Join-Path $env:UserProfile "\Documents\website\launchmenot.lime49.com\releases\")
Set-Variable -name DIRRELEASE 		-value (Get-Location)
Set-Variable -name DIRHELP 		    -value (Join-Path $env:UserProfile "\Documents\Coding\LaunchMeNot\Docs")
Set-Variable -name srcExportDir	    -value (Join-Path $DIRRELEASE ("\dist\"+$DistName+"-src"))

Select-String -path .\launchmenot.nsi -pattern "^!define VERSION ""([\d\w.]+)""" | ForEach{ $_.matches[0].groups[1].value } | Set-Variable -name NewVersion
Select-String -path .\launchmenot.nsi -pattern "^!define NAME ""(\w+)""" | ForEach{ $_.matches[0].groups[1].value } | Set-Variable -name InstallerName

Write-Host ("Creating distribution for "+$InstallerName+" v"+$NewVersion)
Set-Variable -name CurrentDir -value (Get-Location)
$tempDir = Join-Path $DIRRELEASE "\temp"

# Cleanup after last time
Get-ChildItem * -include ($DistName+".zip"),($DistName+"*.exe") | Remove-Item
Remove-Item $tempDir -Recurse -ErrorAction SilentlyContinue
Remove-Item (Join-Path $DIRRELEASE "\dist") -Recurse -ErrorAction SilentlyContinue

# Create directories
$requiredDirectories =  (Join-Path $DIRRELEASE "\temp"),`
			(Join-Path $DIRRELEASE "\dist"),`
			(Join-Path $DIRRELEASE ("\dist\"+$DistName+"-zip")),`
	#		$srcExportDir,`
			(Join-Path $DIRRELEASE ("\dist\"+$DistName+"-setup"))
ForEach($dir in $requiredDirectories) {
	New-Item $dir -type directory
}

$tempFiles = (Join-Path $DIRDOTFUSCATED "LaunchMeNot.exe"),
#		(Join-Path $DIRCOMPILE "\lib\*.dll"),
		(Join-Path $DIRCOMPILE "\*.dll"),
		(Join-Path $DIRRELEASE "EULA.txt"),
		(Join-Path $DIRRELEASE "readme.html"),
		(Join-Path $DIRHELP "LaunchMeNot.chm")
ForEach($f in $tempFiles) {
	Copy-Item $f $tempDir
}

Write-Host "Copying Localizations..."
Set-Location $DIRCOMPILE
$directories =  get-childitem | where {$_.mode -match "d"}
ForEach($lang in $directories) {
	Copy-Item $lang $tempDir -recurse
}
Set-Location $CurrentDir

Write-Host "Creating Zip Distribution..."
Copy-Item (Join-Path $tempDir "\*") (Join-Path $DIRRELEASE ("\dist\"+$DistName+"-zip")) -recurse

Write-Host "Creating Installer..."
Invoke-Expression ($NSISExe + " " + $NSISConfig)

$setupDir = (Join-Path $DIRRELEASE ("\dist\"+$DistName+"-setup"))
Copy-Item ($InstallerName + "-" + $NewVersion + ".exe") $setupDir
Copy-Item (Join-Path $DIRRELEASE  "EULA.txt") $setupDir
Copy-Item (Join-Path $DIRRELEASE  "readme.html") $setupDir

$srcPath = (Join-Path $DIRRELEASE ($DistName+"_"+$NewVersion+"-src.zip"))
$zipPath = (Join-Path $DIRRELEASE ($DistName+"_"+$NewVersion+".zip"))
$setupPath = (Join-Path $DIRRELEASE ($DistName+"_"+$NewVersion+"-setup.zip"))

Write-Host "Running Source Export..."
Write-Host ($svnPath + " export " + $DIRSRC + " " + $srcExportDir)
Invoke-Expression ($svnPath + " export " + $DIRSRC + " " + $srcExportDir)
write-host "Done"

Write-Host "Zipping archives..."

#Write-Host ($WinRAR + " a " + $srcExportDir + " " + $WinRARFilter + " " + $WinRAROpts)
Set-Location $srcExportDir
Write-Host "Zipping source code..."
Invoke-Expression ($WinRAR + " a " + $srcExportDir + " " + $WinRARFilter + " " + $DistName + "-src " +$WinRAROpts)
#Invoke-Expression ($WinRAR + " a " + $srcPath + " " + $WinRARFilter + " -x*\bin\* -x*\obj\* " + $WinRAROpts)
Set-Location (Join-Path $DIRRELEASE "\dist")
Write-Host "Zipping no installer..."
Invoke-Expression ($WinRAR + " a " + $zipPath + " " + $WinRARFilter + " " + $DistName + "-zip " + $WinRAROpts)
Write-Host "Zipping installer..."
Invoke-Expression ($WinRAR + " a " + $setupPath + " " + $WinRARFilter + " " + $DistName + "-setup " + $WinRAROpts)
Set-Location $CurrentDir

$a = Read-Host "Done?"
Move-Item ($srcPath,$zipPath,$setupPath) $DIROUTPUT -Force
Remove-Item $tempDir -Recurse